package us.icitap.commandcenter.ui.dispatcher;

import java.util.ArrayList;
import java.util.Date;

import us.icitap.commandcenter.CommandCenterUI;
import us.icitap.commandcenter.business.ejb.persistence.Call;
import us.icitap.commandcenter.business.ejb.persistence.CallManagement;
import us.icitap.commandcenter.business.ejb.persistence.CallStatusEnum;
import us.icitap.commandcenter.listeners.ManageCallListener;
import us.icitap.commandcenter.ui.custom.ButtonsLayout;
import us.icitap.commandcenter.ui.custom.CustBeanFieldGroup;
import us.icitap.commandcenter.ui.custom.CustSaveButton;
import us.icitap.commandcenter.ui.custom.CustTextArea;
import us.icitap.commandcenter.ui.custom.CustTextField;
import us.icitap.commandcenter.ui.custom.NotificationTextField;

import com.vaadin.data.fieldgroup.BeanFieldGroup;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Field;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class StartVerificationWindow extends Window implements ClickListener{

	
	
	private final NotificationTextField notificationTextField = new NotificationTextField("Kush njoftohet per telefonaten");
	private final CustTextField patrolsTextField = new CustTextField("Patrulla qe caktohet per verifikim");
	private final CustTextArea notesTextArea = new CustTextArea("Shenime mbi komunikimin e telefonates");
	private final CustSaveButton saveButton = new CustSaveButton("RUAJ");
	
	CustBeanFieldGroup<CallManagement> binder = new CustBeanFieldGroup<CallManagement>(CallManagement.class);
	private Call call;
	private CallManagement callManagement;
	
	private ManageCallListener manageCallListener;
	
	public StartVerificationWindow(ManageCallListener manageCallListener,Call call) {
		
		this.manageCallListener = manageCallListener;
		this.setCall(call);
		
		
		this.setCaption("Komuniko telefonate per verifikim");
		this.setModal(true);
		this.center();
		this.setResizable(false);

		this.binder.bind(this.notificationTextField, "whoIsNotified");
		this.binder.bind(this.patrolsTextField, "patrols");
		this.binder.bind(this.notesTextArea, "notes");
		
		VerticalLayout vl = new VerticalLayout();
		vl.setMargin(true);
		FormLayout fl = new FormLayout();
		fl.addComponent(notificationTextField);
		fl.addComponent(patrolsTextField);
		fl.addComponent(notesTextArea);
		this.notesTextArea.setWidth("200px");
		vl.addComponent(fl);
		
		final ButtonsLayout buttonsLayout = new ButtonsLayout("500px");
		buttonsLayout.addComponent(saveButton);
		buttonsLayout.setComponentAlignment(saveButton, Alignment.MIDDLE_RIGHT);
		vl.addComponent(buttonsLayout);
		
		//vl.addComponent(saveButton);
		
		this.setContent(vl);
		
		this.saveButton.addClickListener(this);
	
	}
	
	
	public void setCall(Call call){
		this.call = call;
		this.callManagement = new CallManagement(call);
		this.binder.setItemDataSource(this.callManagement);
		
	}
	@Override
	public void buttonClick(ClickEvent event) {
		try {
			binder.commit();
			call.setStartedVerificationDate(new Date());
			call.setStartedVerificationUser(CommandCenterUI.getUser().getUsername());
			callManagement.setRecordDate(new Date());
			callManagement.setRecordUser(CommandCenterUI.getUser().getUsername());
			
			if(call.getCallManagementList() == null)
				call.setCallManagementList(new ArrayList<CallManagement>());
			call.getCallManagementList().add(callManagement);
			call.setActualStatus(CallStatusEnum.WV);
			this.manageCallListener.startCallVerification(call);

		} catch (Exception e) {
			e.printStackTrace();
		}
			
		
	}

	
	
	
}
